import axios from 'axios';

// 创建 axios 实例
const request = axios.create({
  baseURL: '/api',
  timeout: 5000
});

// 请求拦截器
request.interceptors.request.use(
  config => {
    // 可以在这里设置请求头等信息
    return config;
  },
  error => {
    return Promise.reject(error);
  }
);

// 响应拦截器
request.interceptors.response.use(
  response => {
    const res = response.data;
    // 后端返回的数据格式为 { code: 200, data: xxx, message: 'xxx' }
    if (res.code === 200) {
      return res;
    } else {
      return Promise.reject(new Error(res.message || '未知错误'));
    }
  },
  error => {
    return Promise.reject(error);
  }
);

// 字典大类API
export function getDictCategoryPage(params) {
  return request({
    url: '/dict/category/page',
    method: 'post',
    data: params
  });
}

export function getDictCategoryList() {
  return request({
    url: '/dict/category/list',
    method: 'get'
  });
}

export function getDictCategory(id) {
  return request({
    url: `/dict/category/get/${id}`,
    method: 'get'
  });
}

export function createDictCategory(data) {
  return request({
    url: '/dict/category',
    method: 'post',
    data
  });
}

export function updateDictCategory(data) {
  return request({
    url: '/dict/category',
    method: 'put',
    data
  });
}

export function deleteDictCategory(id) {
  return request({
    url: `/dict/category/${id}`,
    method: 'delete'
  });
}

// 字典小类API
export function getDictItemPage(params) {
  return request({
    url: '/dict/item/page',
    method: 'post',
    data: params
  });
}

export function getDictItemList(categoryId) {
  return request({
    url: `/dict/item/list/${categoryId}`,
    method: 'get'
  });
}

export function getDictItem(id) {
  return request({
    url: `/dict/item/${id}`,
    method: 'get'
  });
}

export function createDictItem(data) {
  return request({
    url: '/dict/item',
    method: 'post',
    data
  });
}

export function updateDictItem(data) {
  return request({
    url: '/dict/item',
    method: 'put',
    data
  });
}

export function deleteDictItem(id) {
  return request({
    url: `/dict/item/${id}`,
    method: 'delete'
  });
} 